﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;

namespace aaaSoft.SkinEngine.SkinHelpers
{
    static class GraphicHelper
    {
        #region 画按钮状图形
        /// <summary>
        /// 画按钮状图形
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="ButtonRect"></param>
        /// <param name="BackColor"></param>
        /// <param name="deep"></param>
        public static void DrawRectButton(Graphics graphics, Rectangle ButtonRect, Color BackColor, Single deep)
        {
            if (ButtonRect.Width == 0 || ButtonRect.Height == 0)
            {
                return;
            }

            float num1 = 2f * deep;
            float num2 = 1.0f * deep;
            float num3 = 0.9f * deep;
            float num4 = 1.2f * deep;

            Int32 BorderWidth = 1;
            //Int32 RoundRectWidth = 4;

            Rectangle rect = new Rectangle(ButtonRect.Left, ButtonRect.Top, ButtonRect.Width, Convert.ToInt32((double)(((double)ButtonRect.Height) / 2.0)) + 1);
            LinearGradientBrush brush = new LinearGradientBrush(rect, ColorHelper.GetDeeperColor(BackColor, num1), ColorHelper.GetDeeperColor(BackColor, num2), LinearGradientMode.Vertical);
            graphics.FillRectangle(brush, brush.Rectangle);
            rect = new Rectangle(ButtonRect.Left, Convert.ToInt32((double)(((double)ButtonRect.Height) / 2.0)), ButtonRect.Width, ButtonRect.Height - Convert.ToInt32((double)(((double)ButtonRect.Height) / 2.0)));
            brush = new LinearGradientBrush(rect, ColorHelper.GetDeeperColor(BackColor, num3), ColorHelper.GetDeeperColor(BackColor, num4), LinearGradientMode.Vertical);
            graphics.FillRectangle(brush, brush.Rectangle);
            brush.Dispose();

            Pen pen = new Pen(ColorHelper.GetDeeperColor(BackColor, 0.9f), (float)BorderWidth);
            rect = new Rectangle(ButtonRect.Left, ButtonRect.Top, ButtonRect.Width - 1, ButtonRect.Height - 1);
            graphics.DrawRectangle(pen, rect);
            /*
            graphics.DrawArc(pen, ButtonRect.Left, ButtonRect.Top, RoundRectWidth, RoundRectWidth, -180, 90);
            graphics.DrawArc(pen, (ButtonRect.Width - RoundRectWidth) - 2, 0, RoundRectWidth, RoundRectWidth, -90, 90);
            graphics.DrawArc(pen, (ButtonRect.Width - RoundRectWidth) - 2, (ButtonRect.Height - RoundRectWidth) - 2, RoundRectWidth, RoundRectWidth, 0, 90);
            graphics.DrawArc(pen, ButtonRect.Left, (ButtonRect.Height - RoundRectWidth) - 2, RoundRectWidth, RoundRectWidth, 90, 90);
             */
            pen.Dispose();
        }
        #endregion

        #region 获取圆角矩形区域
        /// <summary>
        /// 获取圆角矩形区域
        /// </summary>
        /// <param name="nLeftRect"></param>
        /// <param name="nTopRect"></param>
        /// <param name="nRightRect"></param>
        /// <param name="nBottomRect"></param>
        /// <param name="nWidthEllipse"></param>
        /// <param name="nHeightEllipse"></param>
        /// <returns></returns>
        public static Region CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse)
        {
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();

            gp.AddArc(new Rectangle(nLeftRect, nTopRect, nWidthEllipse, nHeightEllipse), 180, 90);
            gp.AddArc(new Rectangle(nRightRect - nWidthEllipse - 1, nTopRect, nWidthEllipse, nHeightEllipse), -90, 90);
            gp.AddArc(new Rectangle(nRightRect - nWidthEllipse - 1, nBottomRect - nHeightEllipse - 1, nWidthEllipse, nHeightEllipse), 0, 90);
            gp.AddArc(new Rectangle(nLeftRect, nBottomRect - nHeightEllipse - 1, nWidthEllipse, nHeightEllipse), 90, 90);
            gp.CloseAllFigures();

            Region rgn = new Region(gp);
            return rgn;
        }
        #endregion

        #region 根据矩形区域，获取合适的显示字符串
        /// <summary>
        /// 根据矩形区域，获取合适的显示字符串
        /// </summary>
        /// <param name="srcStr">源字符串</param>
        /// <param name="rect">矩形区域</param>
        /// <returns></returns>
        public static string GetAppropriateString(Graphics g, string srcStr, Font font, RectangleF rect)
        {
            if (string.IsNullOrEmpty(srcStr)) return srcStr;

            SizeF strSize = g.MeasureString(srcStr, font);
            if (strSize.Width <= rect.Width)
            {
                return srcStr;
            }
            else
            {
                int newLength = (int)(srcStr.Length * rect.Width / strSize.Width);
                string justStr = srcStr.Substring(0, newLength);

                string rtnStr = string.Empty;

                for (int i = justStr.Length - 1; i >= 0; i--)
                {
                    if (i == 0)
                    {
                        rtnStr = justStr.Substring(0, 1) + "...";
                        break;
                    }

                    string tmpStr = justStr.Substring(0, i) + "...";
                    strSize = g.MeasureString(tmpStr, font);
                    if (strSize.Width <= rect.Width)
                    {
                        rtnStr = tmpStr;
                        break;
                    }
                }
                return rtnStr;
            }
        }
        #endregion
    }
}
